class ListNode(object):
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution(object):
    def reverseEvenLengthGroups(self, head):
        index = 1
        mut_head = ListNode(0, head)
        pre = mut_head
        c = 0
        node = head
        d = {}
        while node:
            c += 1
            d[c] = node
            if c == index:
                if index % 2 == 0:
                    next_node = node.next
                    for i in range(index, 0, -1):
                        pre.next = d[i]
                        pre = pre.next
                    pre.next = next_node
                else:
                    pre = node
                index += 1
                c = 0
                d = {}
            node = node.next
        if c > 0 and index % 2 == 0:
            for i in range(c, 0, -1):
                pre.next = d[i]
                pre = pre.next
            pre.next = None
        return mut_head.next





